<%@page import="net.sf.json.JSONArray"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.sql.*"%>
<%@page import="info.test"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="./test.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
	ArrayList<test> alt=new ArrayList<test>();
	test tt=null;
	String sqls1="SELECT * FROM test";
	Connection con=(Connection) application.getAttribute("con");
	Statement sm=con.createStatement();
	ResultSet rs=sm.executeQuery(sqls1);
	int len=-1;
	if(rs==null) len=0;
	else{
		rs.last();
		len=rs.getRow();
		rs.beforeFirst();
	}
	for(int i=0;i<len;++i){
		rs.next();
		tt=new test();
		tt.id=rs.getInt(1);
		tt.caption=rs.getString(2);
		tt.state=rs.getInt(3);
		tt.user=rs.getString(4);
		tt.time=rs.getInt(5);
		tt.teacher=rs.getString(6);
		tt.student=rs.getString(7);
		alt.add(tt);
	}
%>
</head>
<body>
<center>
<h3><font color="red">考试管理</font></h3>
<br/>
<table border="1">
	<tr>
		<th>序号</th>
		<th>标题</th>
		<th>状态</th>
		<th>所属人</th>
		<th>考试时间</th>
		<th>参与的教师</th>
		<th>参与的考生</th>
		<th>操作</th>
	</tr>
<%
	if(len==0){
		out.println("<tr><td colspan=\"8\"><font color=\"red\"><center>没有考试!</center></font></td></tr>");
	}else{
		Iterator<test> iter=alt.iterator();
		for(int i=0;i<len;++i){
			out.println("	<tr>");
			test t=iter.next();
			out.println("		<td>"+t.id+"</td>");
			out.println("		<td>"+t.caption+"</td>");
			out.println("		<td>"+t.state+"</td>");
			out.println("		<td>"+t.user+"</td>");
			out.println("		 <td>"+t.time+"</td>");
			String teachers_info="";
			if(t.teacher.equals(null)== true || t.teacher.equals("")==true){
				teachers_info="没有参与的教师";
			}else{
				JSONObject jot=JSONObject.fromObject(t.teacher);
				JSONArray jat=jot.getJSONArray("teacher");
				int jatlen=jat.size();
				if(jatlen>10){
					for(int j=0;j<10;++j){
						teachers_info=teachers_info+jat.getJSONObject(j).getString("name")+",";
					}
					teachers_info=teachers_info+"……,共"+jatlen+"人。";
				}else{
					for(int j=0;j<jatlen;++j){
						teachers_info=teachers_info+jat.getJSONObject(j).getString("name")+",";
					}
					teachers_info=teachers_info+"共"+jatlen+"人。";
				}
			}
			out.println("		<td>"+teachers_info+"</td>");
			String student_info="";
			if(t.student.equals(null)==true || t.student.equals("")==true){
				student_info="没有参与的学生";
			}else{
				JSONObject jos=JSONObject.fromObject(t.student);
				JSONArray jas=jos.getJSONArray("student");
				int jaslen=jas.size();
				if(jaslen>10){
					for(int j=0;j<10;++j){
						student_info=student_info+jas.getJSONObject(j).getString("name")+",";
					}
					student_info=student_info+"……,共"+jaslen+"人。";
				}else{
					for(int j=0;j<jaslen;++j){
						student_info=student_info+jas.getJSONObject(j).getString("name")+",";
					}
					student_info=student_info+"共"+jaslen+"人。";
				}
			}
			out.println("		<td>"+student_info+"</td>");
			out.println("		<td>");
			out.println("			<button onclick=\"javascript:window.location='./edit_test.jsp?id="+t.id+"'\">编辑考试</button>");
			out.println("			<button onclick=\"remo_test("+t.id+")\">删除考试</button>");
			out.println("	</tr>");
		}
	}
%>
</table>
</center>
</body>
</html>